package com.dongzili.auth.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.dongzili.auth.UserService;
import com.dongzili.common.entity.User;
import com.dongzili.common.controller.BaseController;
import com.dongzili.common.entity.vo.UpdatePwdVo;
import com.dongzili.common.mybatisplus.MybatisPlusUtil;
import com.dongzili.common.utils.BCryptUtils;
import com.dongzili.common.vo.PageRequest;
import com.dongzili.common.vo.R;
import com.dongzili.common.vo.ResponseCode;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController extends BaseController<User> {

    @SaCheckPermission(orRole = {"super-admin"})
    @Override
    public R<Integer> add(@RequestBody User entity) {
        if (!StringUtils.hasText(entity.getPassword()) || !StringUtils.hasText(entity.getUsername())){
            R.error(ResponseCode.PARAM_ERROR);
        }
        entity.setId(null);
        if (entity.getRoleIds() == null || entity.getRoleIds().isEmpty()){
            entity.setRoleIds(Collections.singletonList(1001));
        }
        String encryptPassword = BCryptUtils.encryptByBCrypt(entity.getPassword());
        entity.setPassword(encryptPassword);
        return super.add(entity);
    }

    /**
     * 根据ID更新用户信息,包括密码,权限:super-admin
     * @param entity
     * @return
     */
    @SaCheckPermission(orRole = {"super-admin"})
    @Override
    public R<Integer> update(@RequestBody User entity) {
        if (StringUtils.hasText(entity.getPassword())){
            String encryptPassword = BCryptUtils.encryptByBCrypt(entity.getPassword());
            entity.setPassword(encryptPassword);
        }
        return super.update(entity);
    }

    /** 更新登录者的密码 */
    @PostMapping("/updatePwd")
    public R<Integer> updatePwd(@RequestBody UpdatePwdVo pwd){
        if (!StringUtils.hasText(pwd.getNewPassword()) || !StringUtils.hasText(pwd.getOldPassword())){
            R.error(ResponseCode.PARAM_ERROR);
        }
        Integer userId = StpUtil.getLoginIdAsInt();
        User user = MybatisPlusUtil.getMapper(User.class).selectById(userId);
        if (!BCryptUtils.checkByBCrypt(pwd.getOldPassword(), user.getPassword())){
            return R.error("旧密码错误");
        }
        String encryptPassword = BCryptUtils.encryptByBCrypt(pwd.getNewPassword());
        user.setPassword(encryptPassword);
        return R.success(MybatisPlusUtil.getMapper(User.class).updateById(user));
    }
    /** 删除用户,权限:super-admin */
    @SaCheckPermission(orRole = {"super-admin"})
    @Override
    public R<Integer> delete(@PathVariable Integer id) {
        return super.delete(id);
    }

    @Override
    public R<User> get(@PathVariable Integer id) {
        R<User> r = super.get(id);
        UserService.fillUser(r.getData());
        return r;
    }

    /**
     * 列表,权限:super-admin
     * @param request
     * @return
     */
    @SaCheckPermission(orRole = {"super-admin"})
    @Override
    public R<List<User>> list(@RequestBody PageRequest<User> request) {
        R<List<User>> r =super.list(request);
        UserService.fillUsers(r.getData());
        return r;
    }

}
